package org.lex.net.log;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;

public class StreamLog extends AbstractLog {
	private final PrintWriter out;
	private final StringBuilder buf = new StringBuilder();

	public StreamLog(OutputStream out) {
		this.out = new PrintWriter(out, true);
	}

	public StreamLog(Writer out) {
		this.out = new PrintWriter(out, true);
	}

	protected String format(Type type, Object source, Object message) {
		buf.setLength(0);
		buf.append('[').append(type.name()).append("] ");
		if (null != source)
			buf.append(source).append(" : ");
		buf.append(message);
		return buf.toString();
	}

	public Log log(Type type, Object source, Object message) {
		out.println(this.format(type, source, message));
		return this;
	}
}
